//
//  ZMainViewController.swift
//  ZHweibo
//
//  Created by Henry on 3/21/16.
//  Copyright © 2016 Henry. All rights reserved.
//

import UIKit

class ZMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 自定义tabBar,添加加号按钮,系统的tabBar是只读的.不能直接使用=赋值,使用KVC来赋值
        //        tabBar = CZMainTabBar()
        self.setValue(ZHMainTabBar(), forKey: "tabBar")
        //创建所有的控制器
       addChildViewController()
      
        
        
    }

    
    // 创建所有的控制器
    func addChildViewController(){
        //首页
        let homeVC=ZHHomeViewController()
        addChildViewController(homeVC, title:"首页", imageName: "tabbar_home")
        
        //消息
        let messageVC=ZHMessageViewController()
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        //发现
        let discoverVC=ZHDiscoverViewController()
        addChildViewController(discoverVC, title: "发现", imageName:"tabbar_discover")
        
        //我
        let profileVC=ZHMeViewController()
        addChildViewController(profileVC, title: "我", imageName:"tabbar_profile")
        
    }

    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    /**
    添加为tabBar的子控制器
    - parameter childController: 控制器
    - parameter title:      标题
    - parameter imageName:  图片名称
    */

    func addChildViewController(childController: UIViewController,title:String,imageName:String) {
        // 设置标题
        childController.title=title
        // 设置普通图片
        childController.tabBarItem.image=UIImage(named:imageName)
        // 拼接图片名称   tabbar_profile_highlighted
        let HighlightImageName=imageName+"_highlighted"
        // 设置高亮图片.默认情况下选中图片会被渲染成蓝色的.想要使用图片原来的颜色
        childController.tabBarItem.selectedImage=UIImage(named: HighlightImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
         // 设置item文字颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
        // 添加子控制器
        addChildViewController(UINavigationController(rootViewController: childController))
        
    }

}
